<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="商品名称">
                <a-input allow-clear placeholder="商品名称" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item>
                <div class="select">
<!--                  <a-input allow-clear placeholder="请输入手机号" v-model="queryParam.mobile"></a-input>-->
                  <a-button type="primary" @click="getList">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="0"> 未审核 </a-radio-button>
          <a-radio-button value="1"> 已通过 </a-radio-button>
          <a-radio-button value="2"> 已拒绝 </a-radio-button>
        </a-radio-group>
      </div>
      <a-alert type="info" show-icon style="margin: 10px 0" v-if="selectedRowKeys.length">
        <span slot="message">
          已选择 <span style="color: red">{{ selectedRowKeys.length }}</span> 项
        </span>
      </a-alert>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.pic_url" alt="" />
            </div>
            <div class="info">
              <span>{{ record.name }}</span>
              <span>{{ getCatText(record.cat) }}</span>
            </div>
          </div>
        </div>

        <div slot="send_type" slot-scope="text, record">
          {{ getSendType(text) }}
        </div>

        <span slot="price" slot-scope="text"> ￥{{ text || 0 }} </span>
        <span slot="original_price" slot-scope="text"> ￥{{ text || 0 }} </span>
        <span slot="desc" slot-scope="text"> {{ text || '-' }} </span>

        <div slot="status" slot-scope="text, scope">
          <div class="tagBox">
            <span class="tag" :style="text | orderStatus"></span>
            <span>{{ getStatus(text) }} </span>
          </div>
        </div>

        <div slot="action" slot-scope="scope">
          <a-button type="link" @click="see(scope.attrs)">查看规格</a-button>
        </div>
      </a-table>
    </a-card>

    <ContentDetails :rowKey="i" title="规格" ref="pop" @cancel="attrs = []">
      <a-table :data-source="attrs" :columns="columns2" :pagination="false" bordered>
        <span slot="pic" slot-scope="text">
          <div class="picBox">
            <img v-if="text" class="pic" src="text" alt="" />
            <span v-else>-</span>
          </div>
        </span>
        <span slot="attr_list" slot-scope="text"> {{ text.map(el => el.attr_name).join('-') }} </span>

        <span slot="no">{{ text || '-' }}</span>
      </a-table>
    </ContentDetails>
  </page-header-wrapper>
</template>

<script>
import { goodsAuditList, changeGoodStatus } from '@/api/purchase/supplier'
import tabList from '@/views/saas/components/tabList.vue'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80
  },
  {
    title: '商品名称',
    width: 300,
    scopedSlots: {
      customRender: 'name'
    }
  },
  {
    title: '产品类型',
    dataIndex: 'send_type',
    scopedSlots: {
      customRender: 'send_type'
    }
  },
  {
    title: '售价',
    dataIndex: 'price',
    scopedSlots: {
      customRender: 'price'
    },
    sorter: (a, b) => a.price - b.price
  },
  {
    title: '零售价',
    dataIndex: 'original_price',
    scopedSlots: {
      customRender: 'price'
    },
    sorter: (a, b) => a.original_price - b.original_price
  },
  {
    title: '库存',
    dataIndex: 'num',
    sorter: (a, b) => a.num - b.num
  },
  {
    title: '审核状态',
    dataIndex: 'status',
    scopedSlots: {
      customRender: 'status'
    }
  },
  {
    title: '拒绝原因',
    dataIndex: 'desc',
    scopedSlots: {
      customRender: 'desc'
    }
  },
  {
    title: '时间',
    dataIndex: 'created_at',
    key: 'created_at'
  },
  {
    title: '操作',
    width: 100,
    scopedSlots: {
      customRender: 'action'
    }
  }
]

const columns2 = [
  {
    title: '图片',
    dataIndex: 'pic',
    key: 'pic',
    scopedSlots: {
      customRender: 'pic'
    }
  },
  {
    title: '规格',
    dataIndex: 'attr_list',
    key: 'attr_list',
    scopedSlots: {
      customRender: 'attr_list'
    }
  },
  {
    title: '库存',
    dataIndex: 'num',
    key: 'num',
    sorter: (a, b) => a.num - b.num
  },
  {
    title: '价格',
    dataIndex: 'price',
    key: 'price',
    sorter: (a, b) => a.num - b.num
  },
  {
    title: '货号',
    key: 'no',
    dataIndex: 'no',
    scopedSlots: {
      customRender: 'no'
    }
  }
]

export default {
  components: {
    ContentDetails: () => import('@/components/tools/ContentDetail'),
    tabList
  },

  computed: {
    getCatText() {
      return cat => {
        if (cat && cat.length) {
          return cat.map(el => el.name).join('/')
        } else {
          return '-'
        }
      }
    },

    getSendType() {
      return send_type => {
        let str

        switch (parseInt(send_type)) {
          case 0:
            str = '批发/铺货'
            break

          case 1:
            str = '铺货'
            break

          case 2:
            str = '批发'
            break
        }

        return str
      }
    },

    getStatus() {
      return is_audit => {
        let str

        switch (parseInt(is_audit)) {
          case 0:
            str = '审核中'
            break

          case 1:
            str = '已通过'
            break

          case 2:
            str = '已拒绝'
            break
        }

        return str
      }
    }
  },

  data() {
    return {
      tabList: [
        {
          key: 'saasSupplierPurchaseAuditListList',
          name: '审核列表'
        }
      ],

      // 搜索参数
      queryParam: {
        page: 1,
        limit: 10,
        name: '',
        status: '-1',
        goods_id: ''
      },

      selectedRowKeys: [],
      columns,
      columns2,
      list: [],
      attrs: []
    }
  },

  created() {
    this.getList()
  },

  filters: {
    orderStatus(val) {
      if (val == 0) {
        return 'background-color:#faad14;'
      }

      if (val == 1) {
        return 'background-color:#65c817;'
      }

      if (val == 2) {
        return 'background-color:#ff0000;'
      }
    }
  },

  methods: {
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getList()
    },

    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await goodsAuditList(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.list
        this.totalCount = res.data.count
      }
      hide()
    },

    // 切换商品上下架删除状态
    async changeStatus(e, scope) {
      let status

      switch (e) {
        case true:
          status = 1
          break

        case false:
          status = 0
          break

        default:
          status = e
          break
      }

      const res = await changeGoodStatus({ goods_id: scope.id, status })
      if (res.code == 0) {
        this.getList()
      }
    },

    see(attrs) {
      this.$refs.pop.visible = true
      this.attrs = attrs
    }
  }
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    margin-top: 11px;
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
  .ant-btn {
    margin-top: 11px;
  }
}
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}

.tagBox {
  display: flex;
  align-items: center;
}

.tag {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;
}

.picBox {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50px;
  height: 50px;

  img {
    width: 50px;
    height: 50px;
  }
}
</style>
